export const baseUrl = 'http://121.40.93.39:8903'
// import "../static/js/vconsole.min.js"
// import "../static/js/ihgAppJssdk.js"

function getHeaders() {
	// uni.setStorageSync('Authorization',
	// 	'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI1NzEwMDA2ODQ2MCIsIm5hbWUiOiLpu4RzaXIiLCJja3VzaSI6IjU2MDQ5MDNmM2Y0MjQ4MTlhNmRhMWNjZTY0MDk5NDZkIiwibW9iaWxlUGhvbmUiOiIxNTk5NTA2MDk1NyIsImV4cCI6MTcxNjI2ODgxM30.96Y6g0WmrImUc9rQG5-4FoFgne2XRbsyyU5Dj6Fxo-c'
	// ); //token
	let token = uni.getStorageSync('Authorization')
	let header = {
		"Authorization": token, // 'X-Requested-With': 'XMLHttpRequest',
		'Content-Type': 'application/json; charset=UTF-8'
	}
	return header
}
// function reLogin(){
// 	// 获取当前的url地址
// 	window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx77807363720bae59&redirect_uri=http://ck.kml169.com/wsite&response_type=code&scope=SCOPE&state=STATE#wechat_redirect';
// }
function reLogin() {
	ihgAppJssdk.user.getoToken({
		appId: "HG_GYL_MFP",
		success: function(data) {
			var encryption = data.data
            
			console.log(encryption, 'encryption')
			uni.request({
				url: 'http://121.40.93.39:8903/api/ihg/getUser',
				method: 'post',
				data: {
					"oToken": encryption
				},

				success: function(res) {
					uni.setStorageSync('Authorization', res.data.data.token); //token
				},
				fail: function(res) {
					console.log(res, '222222')
				}
			})

		},
		fail: function(res) {
			console.log(res, 'fail')
		}
	})
}
export default {
	get(url, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'GET',
				url: baseUrl + url,
				data: param,
				header: getHeaders()
			}).then(res => {
				console.log(res, '222222222222222222222')
				switch (res.data.code) {
					case 0:
						resolve(res.data)
						break

					case 5:
						reLogin()
						resolve(res[1])
						break
					default:
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						})
						resolve(res.data)
				}
			}).catch(
				(response) => {
					reject(response)
				}
			)
		})
	},

	post(url, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'POST',
				url: baseUrl + url,
				data: param,
				header: getHeaders()
			}).then(res => {
				console.log(res, '333333333333333333333333')
				switch (res.data.code) {
					case 0:
						resolve(res.data)
						break

					case 5:
						reLogin()
						resolve(res[1])
						break
					default:
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						})
						resolve(res.data)
				}
			}).catch(
				(response) => {
					reject(response)
				}
			)
		})
	},
	put(url, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'PUT',
				url: baseUrl + url,
				data: param,
				header: getHeaders()
			}).then(res => {
				console.log(res, '333333333333333333333333')
				switch (res.data.code) {
					case 0:
						resolve(res.data)
						break
					case 5:
						reLogin()
						resolve(res[1])
						break
					default:
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						})
						resolve(res.data)
				}
			}).catch(
				(response) => {
					reject(response)
				}
			)
		})
	},
	del(url, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'DELETE',
				url: baseUrl + url,
				data: param,
				header: getHeaders()
			}).then(res => {
				console.log(res, '222222222222222222222')
				switch (res.data.code) {
					case 0:
						resolve(res.data)
						break

					case 5:
						reLogin()
						resolve(res[1])
						break
					default:
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						})
						resolve(res.data)
				}
			}).catch(
				(response) => {
					reject(response)
				}
			)
		})
	},
}